<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      Array.prototype.myreduce = function (callback, init) {
        //2个参数, pre的第一次是初始值  cur才是数组的第1个值
        //1个参数, pre的值是 数组的第一个 ,cur是数组的第2个
        let pre = arguments.length == 1 ? this[0] : init
        let index = arguments.length == 1 ? 1 : 0
        for (let i = index; i < this.length; i++) {
          pre = callback(pre, this[i], i, this)
        }
        return pre
      }
      let res = arr.myreduce(function (pre, cur, index) {
        console.log(index)
        return pre + cur
      })
      console.log(res)

      arr.reduce(function (pre, cur, index) {
        console.log(index)
      })
      const arr = [1, 2, 3, 4, 5]
    </script>
  </body>
</html>
